package cn.edu.nju.software.action.order;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.edu.nju.software.bean.show.OrderShow;
import cn.edu.nju.software.bean.storage.Order;
import cn.edu.nju.software.service.intf.OrderService;
import cn.edu.nju.software.util.Constants;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class OperateOrderAction extends ActionSupport implements SessionAware {

	private int id;
	private int code;
	private OrderService orderService;
	private Map<String, Object> session;
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	@Override
	public String execute() throws Exception {
		orderService.modifyOrderStatus(id, code);
		@SuppressWarnings("unchecked")
		Map<Integer, OrderShow> ordersMap = (Map<Integer, OrderShow>) session.get(Constants.SESSION_ORDERS);
		OrderShow orderShow;
		if (ordersMap != null && (orderShow = ordersMap.get(id)) != null) {
			Order order = orderService.getOrderById(id);
			orderShow.setStatus(order.getStatus());
			orderShow.setSubmitTime(order.getSubmitTime());
			orderShow.setDispatchTime(order.getDispatchTime());
			orderShow.setConfirmTime(order.getConfirmTime());
		}
		return SUCCESS;
	}

}
